package org.alex.nettychat.netty.custom.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.alex.nettychat.netty.custom.config.Attributes;
import org.alex.nettychat.netty.custom.protocol.LoginResponsePacket;
import org.alex.nettychat.netty.custom.session.Session;

import java.time.LocalDateTime;

/**
 * @author : alex
 */
public class LoginResponseHandler extends SimpleChannelInboundHandler<LoginResponsePacket> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginResponsePacket loginResponsePacket) throws Exception {
        if (loginResponsePacket.getSuccess()) {
            System.out.println(LocalDateTime.now() + ":客户端登录成功");
            ctx.attr(Attributes.SESSION).set(new Session(loginResponsePacket.getUserId(), "self"));
        } else {
            System.out.println(LocalDateTime.now() + ":客户端登录失败,原因:" + loginResponsePacket.getReason());
        }
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("客户端连接被关闭");

    }
}
